<?php
require_once(dirname(__FILE__) ."/Error.php");
/*
Core class provides basic functionality for other classes.
*/

class Core extends Error
{
    // private vars
    var $_dbLink;
    var $_debug = false; // true, false // conserved for backguard compatibillity
    var $_debugLevel = 1; // 1 stop on errors, 2 show some info, 3 shows everything
    private $_mysqlQueryResult = null;


    /*
    * Constructor
    */
    function __construct() {}


    /*
    * Public Methods
    */
    // setDBLink($dbl)
    public function setDBLink($dbl)
    {
        $this->_dbLink = $dbl;
    }

    /*
    * getDBLink()
    * returns the dbLink
    */
    public function getDBLink()
    {
        return $this->_dbLink;
    }

    /**
    * Transform array with conditions to SQL query
    * Array structure:
    * array('and' => array('id = 0', 'id2 >= 3'), 'or' => array('sec = \'www\'', 'sec2 <> \'erere\'')), etc
    * where array key - condition (AND, OR, etc), value - condition string.
    *
    * @param array $condition
    * @param string $prefix
    * @param bool $where - True - yes, flase - not
    * @return string
    */
    public function _prepareCondition($condition, $where = false, $prefix = '') {
        if (!is_array($condition)) {
            return $condition;
        }
        $sql = ' ';
        if (TRUE === $where) {
            $sql .= 'WHERE ' . $prefix;
        }
        $keys = array_keys($condition);
        for ($i = 0; $i < count($keys); $i++) {
            if (FALSE === $where || (TRUE === $where && $i > 0)) {
                $sql .= ' ' . strtoupper($keys[$i]) . ' ' . $prefix;
            }
            $sql .= implode(' '. strtoupper($keys[$i]) .' '. $prefix, $condition[$keys[$i]]);
        }
        return $sql;
    }


    /*
    * Private Methods
    */
    public function _checkDBLink()
    {
        if (!$this->_dbLink) return $this->_error("Please, provide a valid MySQL resource identifier.");
        return 1;
    }

    public function _debugLog($lev, $msg)
    {
        if ($this->_debugLevel==$lev) echo $msg ."\n<br>";
    }


    /**
    * Execute a simple MySQL query. Might be used with insert, update and delete queries.
    * the mysql result identifier is stored int the $result var if is passed (should be by reference).
    *
    * @param string   $sql
    * @param mysql-resource $result passed by reference
    * @return integer 1|0
    */
    public function _mysqlQuery($sql, &$result=null)
    {
        $this->_debugLog(3,get_class($this) ."->_mysqlQuery($sql, $result)");
        if (!$this->_checkDBLink()) return 0;

        $result = @mysql_query($sql, $this->_dbLink);

        if (!$result && mysql_errno($this->_dbLink) && $GLOBALS["CONF"]["dataobjedt_show_mysq_errors"])
        {
            $msg  = '<style> #mysqlErrorMsg { -moz-border-radius:10px; background:#FFFFFF none repeat scroll 0 0 !important; border:1px solid red; margin:5px; padding:5px; } #mysqlErrorMsg td {font-family: Tahoma, Geneva, sans-serif; font-size:12px; vertical-align: top} #mysqlErrorMsg strong {color: #DF0000 !important;} </style>';
            $msg .= '<table cellpadding="3" id="mysqlErrorMsg"><tr>';
            $msg .= '<td colspan="2" align="center"><strong>WARNING: MySQL ERROR</strong></td></tr>';
            $msg .= '<tr><td width="100"><b>MySQL said:</b></td><td>'. mysql_error($this->_dbLink) .': '. mysql_error($this->_dbLink) .'</td></tr>';
            $msg .= '<tr><td><b>MySQL Query:</b></td><td>'. $sql .'</td></tr></table>';
            $this->_debugLog(1, $msg);
            return 0;
        }
        return 1;
    }


    /*
    * Process a string values and prepares it for a SQL query
    * txt2sql()
    */
    public function txt2sql($str)
    {
        $this->_debugLog(2, get_class($this) ."->txt2sql()");
        if (!$this->_checkDBLink()) return 0;
        if(get_magic_quotes_gpc()) $str = stripslashes($str);
        $str = trim($str);
        return mysql_real_escape_string($str, $this->_dbLink);
    }
}

?>
